package org.huzhp.gateway.service;

import org.huzhp.gateway.service.impl.Log4jAuditLogService;
import org.huzhp.gateway.service.impl.RestServiceAuditLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: java类作用描述
 * @Author: huzp
 * @CreateDate: 2020/4/13 13:38
 * @Version: 1.0
 */
@Configuration
@ConfigurationProperties("spring.cloud.gateway.log")
public class AuditLogConfig {

    private int threadSize = 5;

    private String type;

    private String server;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public ExecutorService executor() {
        return Executors.newFixedThreadPool(threadSize);
    }

    @Bean
    public AbstractAuditLogService auditLogService() {
        AbstractAuditLogService auditLogService = null;
        if (LogType.REST.name().equalsIgnoreCase(type)) {
            Assert.notNull(server, "config rest log,the server param is not null");
            auditLogService = new RestServiceAuditLogService(executor(), server, restTemplate);
        } else {
            auditLogService = new Log4jAuditLogService(executor());
        }
        return auditLogService;
    }

    public enum LogType {
        DEFAULT, REST
    }

    public int getThreadSize() {
        return threadSize;
    }

    public void setThreadSize(int threadSize) {
        this.threadSize = threadSize;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }
}
